package com.sc.web.gwt.client.view.agentwidget.contents;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiConstructor;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import com.sc.web.gwt.client.service.UserAccountDataManagementService;
import com.sc.web.gwt.client.service.UserAccountDataManagementServiceAsync;
import com.sc.web.gwt.client.view.administratorwidget.AdministratorWidgetResources;
import com.sc.web.gwt.shared.model.UserAccount;

import java.util.ArrayList;
import java.util.List;

public class ViewAllUsersWidget extends Composite {
    private static Binder binder = GWT.create(Binder.class);
    final ViewAllUsersWidget thisWidget = this;

    public interface Binder extends UiBinder<HTMLPanel, ViewAllUsersWidget> {
    }

    UserAccountDataManagementServiceAsync userAccountDataManagementServiceAsync = GWT.create(UserAccountDataManagementService.class);
    private List<UserAccount> allUserAccounts = new ArrayList<UserAccount>();
    final AdministratorWidgetResources resources = GWT.create(AdministratorWidgetResources.class);
    Grid<UserAccount> grid;
    boolean isViewAll = false;
    private ContentPanel gridPanel;

    @UiField
    HTMLPanel topPanel;
    @UiField
    Button searchBtn;
    @UiField
    ListBox searchCB;
    @UiField
    HTMLPanel searchPanel;
    @UiField
    TextBox searchText;

    @UiConstructor
    public ViewAllUsersWidget(boolean isViewAll) {
        initWidget(binder.createAndBindUi(this));
        this.isViewAll = isViewAll;
        if(isViewAll){
            searchPanel.getElement().getStyle().setDisplay(Style.Display.NONE);
            userAccountDataManagementServiceAsync.getAllUserAccounts(
                new AsyncCallback<List<UserAccount>>() {
                    public void onFailure(Throwable throwable) {
                        throwable.printStackTrace();
                    }

                    public void onSuccess(List<UserAccount> userAccounts) {
                        allUserAccounts.addAll(userAccounts);
                        initDataTable();
                    }
                }
            );
        }
        else{
            searchCB.addItem("ID");
            searchCB.addItem("First Name");
            searchCB.addItem("Last Name");
            searchCB.addItem("Account");
            searchCB.addItem("Account Code");
            searchCB.addItem("State");
            searchCB.addItem("Site Name");
            searchCB.addItem("Identify Number");
            searchCB.addItem("Email");
            searchCB.addItem("Address");
            searchCB.addItem("City");
            searchCB.addItem("Nation");


            searchBtn.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    userAccountDataManagementServiceAsync.searchUserAccounts(searchText.getText(), searchCB.getSelectedIndex(), new AsyncCallback<List<UserAccount>>() {
                        public void onFailure(Throwable throwable) {
                            throwable.printStackTrace();
                        }

                        public void onSuccess(List<UserAccount> result) {
                            allUserAccounts = new ArrayList<UserAccount>();
                            allUserAccounts.addAll(result);
                            grid.getStore().removeAll();
                            grid.getStore().add(allUserAccounts);
                            grid.getStore().commitChanges();
                        }
                    });
                }
            });
            initDataTable();
        }
    }

    private void initDataTable() {

        List<ColumnConfig> configs = new ArrayList<ColumnConfig>();
        ColumnConfig column = new ColumnConfig();
        column.setId("userAccountId");
        column.setHeader("ID");
        column.setWidth(50);
        configs.add(column);

        column = new ColumnConfig();
        column.setId("firstName");
        column.setHeader("First Name");
        column.setWidth(70);
        configs.add(column);

        column = new ColumnConfig();
        column.setId("lastName");
        column.setHeader("Last Name");
        column.setWidth(100);
        configs.add(column);

        column = new ColumnConfig();
        column.setId("userAccount");
        column.setHeader("Accounts");
        column.setWidth(100);
        configs.add(column);

        column = new ColumnConfig();
        column.setId("accountCode");
        column.setHeader("Account Code");
        column.setWidth(100);
        configs.add(column);

        column = new ColumnConfig();
        column.setId("state");
        column.setHeader("State");
        column.setWidth(70);
        configs.add(column);

        column = new ColumnConfig();
        column.setId("siteName");
        column.setHeader("Site Name");
        column.setWidth(100);
        configs.add(column);

        column = new ColumnConfig();
        column.setId("identityNumber");
        column.setHeader("Identity Number");
        column.setWidth(70);
        configs.add(column);

        column = new ColumnConfig();
        column.setId("email");
        column.setHeader("Email");
        column.setWidth(100);
        configs.add(column);

        DateField dob = new DateField();
        dob.getPropertyEditor().setFormat(DateTimeFormat.getFormat("dd/MM/yyyy"));

        column = new ColumnConfig();
        column.setId("dob");
        column.setHeader("DOB");
        column.setWidth(70);
        column.setDateTimeFormat(DateTimeFormat.getFormat("dd/MM/yyyy"));
        configs.add(column);

        column = new ColumnConfig();
        column.setId("address");
        column.setHeader("Address");
        column.setWidth(100);
        configs.add(column);

        column = new ColumnConfig();
        column.setId("city");
        column.setHeader("City");
        column.setWidth(70);
        configs.add(column);

        column = new ColumnConfig();
        column.setId("nation");
        column.setHeader("Nation");
        column.setWidth(70);
        configs.add(column);

        final ListStore<UserAccount> store = new ListStore<UserAccount>();
        store.add(allUserAccounts);
        ColumnModel cm = new ColumnModel(configs);

        gridPanel = new ContentPanel();
        gridPanel.setHeading("View All Users");
        gridPanel.setFrame(true);
        gridPanel.setIcon(resources.ICONS.table());

        grid = new Grid<UserAccount>(store, cm);
        grid.setAutoExpandColumn("userAccountId");
        grid.setBorders(true);
        grid.setHeight("100%");
        grid.setTrackMouseOver(true);
        grid.setStripeRows(true);
        gridPanel.add(grid);
        gridPanel.setSize("100%", "500px");
        gridPanel.setLayout(new FitLayout());

        ToolBar toolBar = new ToolBar();
        com.extjs.gxt.ui.client.widget.button.Button addBtn = new com.extjs.gxt.ui.client.widget.button.Button("Add new end user");
        addBtn.addSelectionListener(new SelectionListener<ButtonEvent>() {
            @Override
            public void componentSelected(ButtonEvent ce) {
                userAccountDataManagementServiceAsync.createNewUserAccount(new AsyncCallback<UserAccount>() {
                    public void onFailure(Throwable throwable) {
                        throwable.printStackTrace();
                    }

                    public void onSuccess(UserAccount userAccount) {
                        if (userAccount != null) {
                            new AddOrEditUserAccountWidget(userAccount, thisWidget).showWidget();
                        }
                    }
                });
            }
        });
        toolBar.add(addBtn);
        com.extjs.gxt.ui.client.widget.button.Button editBtn = new com.extjs.gxt.ui.client.widget.button.Button("Edit selected user");
        editBtn.addSelectionListener(new SelectionListener<ButtonEvent>() {
            @Override
            public void componentSelected(ButtonEvent ce) {
                UserAccount userAccount = grid.getSelectionModel().getSelectedItem();
                if (userAccount != null) {
                    new AddOrEditUserAccountWidget(userAccount, thisWidget).showWidget();
                }
            }
        });
        toolBar.add(editBtn);
        gridPanel.setTopComponent(toolBar);

        topPanel.add(gridPanel);
    }

    public void refreshTable(){
        userAccountDataManagementServiceAsync.getAllUserAccounts(
                new AsyncCallback<List<UserAccount>>() {
                    public void onFailure(Throwable throwable) {
                        throwable.printStackTrace();
                    }

                    public void onSuccess(List<UserAccount> userAccounts) {
                        allUserAccounts = new ArrayList<UserAccount>();
                        allUserAccounts.addAll(userAccounts);
                        grid.getStore().removeAll();
                        grid.getStore().add(allUserAccounts);
                        grid.getStore().commitChanges();
                    }
                }
        );
    }

}
